#include "gkt_board.h"
#include "gkt_debug.h"
#include "visual_intercom_common.h"
#include "gkt_visual_intercom.h"

#if defined(GKT_CONFIG_VISUAL_INTERCOM_ENABLE) && GKT_CONFIG_VISUAL_INTERCOM_ENABLE	\
	&& (VISUAL_INTERCOM_LOCATION == GKT_MODULE_LOC_BACKBOARD)

#include "gkt_backboard.h"

enum {
	VISUAL_INTERCOM_RSP_STATUS = GKT_BB_APP_PACKET_MINSIZE,
	VISUAL_INTERCOM_RSP_MAX
};

enum {
	VISUAL_INTERCOM_OP_INFO = 0,
	VISUAL_INTERCOM_OP_STATUS,
	VISUAL_INTERCOM_OP_MAX
};


typedef struct _visual_intercom {
	gkt_visual_intercom_notify notify;
} visual_intercom_s;
static visual_intercom_s s_visual_intercom_bb;

static __RETMEM_USR gkt_visual_intercom_camera_type_e s_visual_intercom_camera_type;

static void visual_intercom_get_info_notify(int type, 
				const uint8_t *data, int length_retval)
{
	uint8_t status;
	if (GKT_BB_NOTIFY_PEER == type) {
		if (length_retval < VISUAL_INTERCOM_RSP_STATUS) {
			gkt_error("visual_intercom_get_info_notify: <PEER> data length invalid!\n");
			return;
		}

		status = data[VISUAL_INTERCOM_RSP_STATUS];
		if (status >= GKT_VISUAL_INTERCOM_CAMERA_TYPE_MAX) {
			gkt_error("visual_intercom_get_info_notify: <PEER> data length invalid!\n");
			return;
		}
		gkt_trace("visual_intercom_get_info_notify: <PEER> type(%u)\n",
			s_visual_intercom_camera_type);
		s_visual_intercom_camera_type = status;
	}
	else if (length_retval < GKT_SUCCESS) {
		gkt_error("visual_intercom_get_info_notify: <LOCAL> retval(%d)\n",
			length_retval);
	}
}
static void visual_intercom_module_notify(int type, 
				const uint8_t *data, int length_retval)
{
	uint8_t opcode;
	gkt_info("visual_intercom_module_notify: type(%d), length_retval(%d)\n", 
		type, length_retval);
	opcode = data[GKT_BB_APP_PACKET_OPCODE];
	if (VISUAL_INTERCOM_OP_INFO == opcode)
		visual_intercom_get_info_notify(type, data, length_retval);
}
int visual_intercom_get_info(void)
{
	uint8_t data[GKT_BB_APP_PACKET_MINSIZE];

	data[GKT_BB_APP_PACKET_MODULE_ID] = GKT_BB_MODULE_VISUAL_INTERCOM;
	data[GKT_BB_APP_PACKET_OPCODE] = VISUAL_INTERCOM_OP_INFO;
	return gkt_backboard_send_data(data, GKT_BB_APP_PACKET_MINSIZE);
}

gkt_visual_intercom_camera_type_e gkt_visual_intercom_get_camera_type(void)
{
	return s_visual_intercom_camera_type;
}
void gkt_visual_intercom_register_notify(
				gkt_visual_intercom_notify notify)
{
	s_visual_intercom_bb.notify = notify;
}
int gkt_visual_intercom_init(void)
{
	s_visual_intercom_bb.notify = NULL;

	gkt_backboard_register_module(GKT_BB_MODULE_VISUAL_INTERCOM, 
			VISUAL_INTERCOM_OP_MAX, visual_intercom_module_notify);

	if (GKT_SYSTEM_RESET_WAKEUP != g_system_info.reset_mode)
		s_visual_intercom_camera_type = GKT_VISUAL_INTERCOM_CAMERA_TYPE_MAX;

	if (GKT_VISUAL_INTERCOM_CAMERA_TYPE_MAX == s_visual_intercom_camera_type)
		visual_intercom_get_info();

	return GKT_SUCCESS;
}

#endif

